﻿using System;
using System.Diagnostics;
using System.IO;
using Windows.Storage.Streams;
using NPortableEngine;
using NCommonLib.Extensions;
using NCommonLib.Thread;
using System.Threading;
using System.Diagnostics.CodeAnalysis;
#if NETFX_CORE

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
using Windows.ApplicationModel;


#else

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.ComponentModel;

#endif

namespace NCache
{
    public static class ImageExtensions
    {
        #region CommonCache

        [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "UriSource is applicable only to Image elements.")]
        public static Uri GetCacheUri(DependencyObject obj)
        {
            if (null == obj)
            {
                throw new ArgumentNullException("obj");
            }
            return (Uri)obj.GetValue(CacheUriProperty);
        }

        [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "UriSource is applicable only to Image elements.")]
        public static void SetCacheUri(DependencyObject obj, Uri value)
        {
            if (null == obj)
            {
                throw new ArgumentNullException("obj");
            }
            obj.SetValue(CacheUriProperty, value);
        }

        public static readonly DependencyProperty CacheUriProperty =
            DependencyProperty.RegisterAttached("CacheUri", typeof(Uri), typeof(ImageExtensions), new PropertyMetadata(null,OnCacheUriChanged));

        private static async void OnCacheUriChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            Uri newUri = (Uri)e.NewValue;            

            Image image = (Image)obj;
#if NETFX_CORE
            if (DesignMode.DesignModeEnabled)
            {
                return;
            }
#else
            if (DesignerProperties.IsInDesignTool)
            {
                image.Source = new BitmapImage(newUri);
            }
#endif
            else
            {
                image.Source = null;
                BitmapImage bitmapImage = new BitmapImage();
                //bitmapImage.UriSource = newUri;
                //Debug.WriteLine("uri:--->" + newUri.ToString());
                using (Stream stream = await CacheEngine.QueueCacheImage(newUri))
                {
                    if (stream != null)
                    {
                        try
                        {
#if NETFX_CORE
                            bitmapImage.SetSource(await stream.ToRandomStream());
#else
                            bitmapImage.SetSource(stream);
#endif
                        }
                        catch (System.Exception ex)
                        {
                            Debug.WriteLine("ImageCache - SetImageSource() - Has Error:" + ex.Message.ToString());
                        }
                    }
                }
                image.Source = bitmapImage;
            }
        }
        #endregion
        
    }
}
